package com.powervision.base.base

import android.app.ProgressDialog
import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import com.aisier.architecture.util.toast
import com.jeremyliao.liveeventbus.LiveEventBus
import com.powervision.http.LOADING_STATE
import com.powervision.http.SHOW_TOAST

/**
 * @auth: njb
 * @date: 2021/11/18 10:27
 * @desc:
 */
abstract class BaseActivity(@LayoutRes contentLayoutId: Int) : AppCompatActivity(contentLayoutId),
    IBaseView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init()
        observeUi()
    }

    private fun observeUi() {
        LiveEventBus.get<String>(SHOW_TOAST).observe(this) {
            toast(it)
        }
        LiveEventBus.get<Boolean>(LOADING_STATE).observe(this) {
            if (it) showLoading() else dismissLoading()
        }
    }

    protected abstract fun init()

    private var progressDialog: ProgressDialog? = null

    override fun showLoading() {
        if (progressDialog == null)
            progressDialog = ProgressDialog(this)
        progressDialog?.show()
    }

    override fun dismissLoading() {
        progressDialog?.takeIf { it.isShowing }?.dismiss()
    }

}